Thread: [beginner] detecting number of UpPeRcAsE letters.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    Question [beginner] detecting number of UpPeRcAsE letters.

    So I've been teaching myself C for a few weeks. Haven't had many problems at all. Just wrote this extremely simple program but it seems to be malfunctioning. It counts correctly the number of uppercase letters in a string so long as i don't use the space-bar. once I add a space it only counts the number of uppercase letters of the first word..
    I'm sure its a nublet mistake, help pl0x. printf("I ..........ing love programming");

    Code:
    #include <stdio.h>
    
    
    main()
    {
            char text[200];
            int i = 0;
            int uppercase = 0;
            
            printf("Enter text:");
            scanf("%s", text);
            
        while(text[i] != '\0')
        {
                if(text[i] > 'A' && text[i] < 'Z')
                {
                        uppercase++;
                }
                
                i++;
        }
        
            printf("there were %d uppercase letters in that text", uppercase);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Put this line of code after your "scanf()":

    Code:
    printf("%s\n",text);
    You'll notice that anything after a space is ignored. If you read up on "scanf()":

    %s: Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
    So "scanf()" used in that way will only get you a single word.

    There's a FAQ for getting a line of text from a user ("fgets()" does not stop reading until it encounters a newline [or until the specified size it reached] - all other white space is stored as expected).

    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Code:
    if(text[i] > 'A' && text[i] < 'Z')
    What if "text[i]" is 'A' or is 'Z'?
    Last edited by Matticus; 03-20-2014 at 02:01 PM.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    Thanks very much for the reply! yeah, I forgot about gets(), however I didnt realise that scanf() cuts off after a space. Also I'll put in the <= and >= now, forgot to do that too. whats the difference between fgets() and gets()?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Digital Android View Post
    whats the difference between fgets() and gets()?
    fgets is much safer. it allows you to specify the size of the buffer you're reading into, to help to avoid buffer overruns.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Digital Android View Post
    Thanks very much for the reply! yeah, I forgot about gets(), however I didnt realise that scanf() cuts off after a space. Also I'll put in the <= and >= now, forgot to do that too. whats the difference between fgets() and gets()?
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Digital Android View Post
    Also I'll put in the <= and >= now, forgot to do that too.
    Note, there is a wonderful function called isupper() that you can use. It is a bit more robust, and makes your code a bit easier to read.

    Regarding the robustness: your solution is fine for ASCII and compatible character sets -- which covers the vast majority of systems out there. Do be aware that there still do exist systems where the letters of the alphabet are not contiguous (e.g. EBCDIC). You will probably never run into this, but it's good to be aware of such things. And since using isupper() does not add complexity to your code or slow it down in any way, you might as well use it. You can Google examples. Make sure you #include <ctype.h> to use it.

    Regarding readability: code that is easier to read means fewer bugs and they are easier to find/fix when they do occur.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    how can we save every uppercase hi finds in a array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2013, 11:12 PM
  2. Remove uppercase case letters from a word
    By Sid_TheBeginner in forum C Programming
    Replies: 12
    Last Post: 10-25-2012, 09:05 PM
  3. Find word with uppercase and lowercase letters
    By doia in forum C Programming
    Replies: 9
    Last Post: 07-15-2010, 08:51 PM
  4. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM

Tags for this Thread